package com.common.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.component.Pagination;
import com.common.manager.SystemInfoManager;
import com.common.model.StaffInfoModel;
import com.common.model.SystemInfoModel;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;
import com.framework.web.BaseController;

@Controller
@RequestMapping(value = "/management/system")
public class SystemInfoController extends BaseController{

	 private static String modelUrl="/management/system/";
	 
    @Autowired
	private SystemInfoManager systemInfoManager;
    
    

    /**
     * 功能：页面分页列表
     * @param vo
     * @param param
     * @return
     */
	@RequestMapping("pageList")
	public ModelAndView pageList(@ModelAttribute("page") Pagination vo,@ModelAttribute("systemInfoModel") SystemInfoModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		List<DataMap> dataList = systemInfoManager.getPageObjectList(vo, param);
		modelView.addObject("vo", vo);
		modelView.addObject("param", param);
		modelView.addObject("objList", dataList);
		return modelView;
	}
	
	/**
	 * 功能：初始化添加页面
	 * @param systemInfoModel
	 * @return
	 */
	@RequestMapping("initAdd")
	public ModelAndView initAdd(@ModelAttribute("systemInfoModel") SystemInfoModel systemInfoModel) {
		ModelAndView modelView = new ModelAndView(modelUrl+"add");
		log.info("add is page");
		modelView.addObject("vo",systemInfoModel);
		return modelView;
	}
	
	/**
	 * 功能：初始化编辑页面
	 * @param intID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/{intID}/initEdit")
	public ModelAndView initEdit(@PathVariable("intID") String intID) {
		ModelAndView modelView = new ModelAndView(modelUrl+"update");
		IData dataMap = new DataMap();
		dataMap.put("intID", intID);
		SystemInfoModel systemInfoModel = (SystemInfoModel)systemInfoManager.getObjectInfo(dataMap);
		modelView.addObject("vo",systemInfoModel);
		return modelView;
	}
	
	
	/**
	 * 功能：执行插入操作
	 * @param systemInfoModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add")
	public String add(HttpServletResponse response,@ModelAttribute("systemInfoModel") SystemInfoModel systemInfoModel) {
		try{
			int opNum = systemInfoManager.insertObjectAndGetID(systemInfoModel);
			if(opNum>0){
				return super.jsonReturn(response,200);
			}else{
				return super.jsonReturn(response,300);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}
	
	/**
	 * 功能：执行更新操作
	 * @param systemInfoModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update")
	public String update(HttpServletResponse response,@ModelAttribute("systemInfoModel") SystemInfoModel systemInfoModel) {
		try{
			int opNum =systemInfoManager.updateObject(systemInfoModel);
			if(opNum>0){
				return super.jsonReturn(response,200);
			}else{
				return super.jsonReturn(response,300);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}
	
	/**
	 * 功能：执行单记录删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/{intID}/delete")
	public String delete(HttpServletResponse response,@PathVariable("intID") int intID) {
		try{
			IData dataMap = new DataMap();
			dataMap.put("intID", intID);
			boolean isFlag = systemInfoManager.checkSystemRoleInfo(dataMap);
			if(!isFlag){
				int opNum =systemInfoManager.deleteObject(dataMap);
				if(opNum>0){
					return super.jsonReturn(response,200,false);
				}else{
					return super.jsonReturn(response,300,false);
				}
			}else{
				return super.jsonReturn(response,300,"请先解除角色关联数据!",false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
	/**
	 * 功能：执行批量删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteAll")
	public String deleteAll(HttpServletResponse response,HttpServletRequest request) {
		try{
			String ids = request.getParameter("ids");
			IData dataMap = new DataMap();
			dataMap.put("ids", ids);
			String message="";
			int opNum =systemInfoManager.deleteObject("deleteAllObject",dataMap);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
	
	/**
	 * 功能：校验系统变慢
	 * @param response
	 * @param request
	 */
	@ResponseBody
	@RequestMapping("/checkSysCode")
	public void checkSysCode(HttpServletResponse response,HttpServletRequest request){
		String sysCode = request.getParameter("sysCode");
		boolean isFlag= true;
		JSONObject rtnMsg = new JSONObject();
		if(sysCode!=null){
				try {
					SystemInfoModel paramModel = new SystemInfoModel();
					paramModel.setSysCode(sysCode);
					SystemInfoModel systemInfoModel = (SystemInfoModel)systemInfoManager.getObjectInfo(paramModel);
					if(systemInfoModel!=null){
						isFlag =false;
						rtnMsg.put("msg", "系统编码已存在请重新输入");
					}else{
						rtnMsg.put("msg", "校验通过");
					}
				} catch (Exception e) {
					isFlag =false;
					rtnMsg.put("msg", "校验发生异常");
				}
		}else{
			rtnMsg.put("msg", "员工工号不能为空");
		}
		rtnMsg.put("isFlag", isFlag);
		this.ajaxJson(response, rtnMsg.toString());
	}
	
}